/**
 * Copyright (C) 2010 altuure <altuure [AT] gmail [DOT] com> http://www.altuure.com/projects/yagdao
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.altuure.yagdao;

import java.io.Serializable;

/**
 * DAO meta class for paging and sorting.
 * @author altuure
 */
public class YLimit implements Serializable {
    private static final long serialVersionUID = 1L;
    /**
     * not assigned value.
     */
    public static final int NOT_ASSIGNED = -1;

    /**
     * first result index.
     */
    protected int firstResult = NOT_ASSIGNED;
    /**
     * result set page size.
     */
    protected int pageSize = NOT_ASSIGNED;

    /**
     * limit result with given properties.
     * @param firstResult first result index
     * @param pageSize page size
     */
    public YLimit(int firstResult, int pageSize) {
        this.firstResult = firstResult;
        this.pageSize = pageSize;
    }

    /**
     * unlimited result size from given index.
     * @param firstResult first result index
     */
    public YLimit(int firstResult) {
        this(firstResult, NOT_ASSIGNED);
    }

    /**
     * default cons.
     */
    public YLimit() {
        this(NOT_ASSIGNED, NOT_ASSIGNED);
    }

    /**
     * first result index.
     * @return first result
     */
    public int getFirstResult() {
        return firstResult;
    }

    /**
     * @return page size.
     */
    public int getPageSize() {
        return pageSize;
    }

    /** is paged.
     * @return firstresult>0*/
    public boolean isPaged() {
        return firstResult >= 0;
    }

    /** is page sized.
     * @return pageSize>0 */
    public boolean isPageSized() {
        return pageSize >= 0;
    }

    /*
     * (non-Javadoc)
     * 
     * @see java.lang.Object#hashCode()
     */
    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + firstResult;

        result = prime * result + pageSize;
        return result;
    }

    /*
     * (non-Javadoc)
     * 
     * @see java.lang.Object#equals(java.lang.Object)
     */
    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        YLimit other = (YLimit) obj;
        if (firstResult != other.firstResult)
            return false;

        if (pageSize != other.pageSize)
            return false;
        return true;
    }

    /*
     * (non-Javadoc)
     * 
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString() {
        return "DAOLimit [firstResult=" + firstResult + ", pageSize=" + pageSize + "]";
    }
}
